package crs.web.validadores;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.validation.Errors;
import org.springframework.validation.Validator;

import crs.empleados.tablebean.JsonJTableEmpleadosBean;
import crs.users.facade.UserFacade;
 
@Component
public class EmpValidator implements Validator {

	@Autowired
	private UserFacade userFacade;
	
    //which objects can be validated by this validator
    @Override
    public boolean supports(Class<?> paramClass) {
        return JsonJTableEmpleadosBean.class.equals(paramClass);
    }
 
    @Override
    public void validate(Object obj, Errors errors) {
         
        JsonJTableEmpleadosBean emp = (JsonJTableEmpleadosBean) obj;
        
        if (emp.getEmpUserId()==null){
			if (userFacade.userExist(emp.getEmpleadoId())) {
				errors.rejectValue("empleadoId", "message.regUser", new Object[]{emp.getEmpleadoId()},"");
	        }
	        String email =emp.getEmpleadoId()+"@"+emp.getEmpleadoId()+".com";
			if (userFacade.emailExist(email)) {  
				errors.rejectValue("empleadoId", "message.regEmail", new Object[]{email},"");
		    }
        }else{
        	if (userFacade.userExist(emp.getEmpleadoId(), emp.getEmpUserId())) {
				errors.rejectValue("empleadoId", "message.regUser", new Object[]{emp.getEmpleadoId()},"");
	        }
	        String email =emp.getEmpleadoId()+"@"+emp.getEmpleadoId()+".com";
			if (userFacade.emailExist(email, emp.getEmpUserId())) {  
				errors.rejectValue("empleadoId", "message.regEmail", new Object[]{email},"");
		    }
        }
		
		
			
		
         
        //ValidationUtils.rejectIfEmptyOrWhitespace(errors, "name", "name.required");
        //ValidationUtils.rejectIfEmptyOrWhitespace(errors, "role", "role.required");
        
        
    }
}